window.onload=function(){
    // var date = document.getElementById("date");
    // var today = new Date();
    // date.valueAsDate = today;
    // jsonAll=sessionStorage.getItem("jsonAll");
    showTable();
};

function changeAlign(){

    var t = document.getElementById("tbody");
    var x =t.getElementsByTagName("td");
    //alert(x.length);
    for (var i = 0; i<x.length; i++) {

        x[i].style = "text-align : center";
    }
}
var jsonAll ;
var json=[] ;
var pageset=7;
var page=1;
function show(json){
    var tb=document.getElementById("tb");

    var length = tb.tBodies[0].rows.length;
    for(var i=0;i<length;++i){
        tb.tBodies[0].deleteRow(0);//每次删除第一行
    }
    for(var i=0;i<json.length;++i){
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML="<input type='checkbox'/>";
        row.insertCell().innerText=json[i].rentOrderId;
        row.insertCell().innerText=json[i].accountNo;
        row.insertCell().innerText=json[i].orderTime.substring(0,json[i].orderTime.length-2);
        row.insertCell().innerText=json[i].consigeeAddr;
        if(json[i].getOrConsigee == "1"){
            row.insertCell().innerText="送货";
        }else{
           row.insertCell().innerText="取货";
        }
        row.insertCell().innerText=json[i].rentTotalMoney;

        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='purchase_edit("+json[i].orderId+")'>"+
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onClick='product_del(this,'10001')'" +
            " href='javascript:;' title='删除'>"+
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";

        row.onclick = function () {
          //  alert($(this.cells[1]).html());

            var orderId = $(this.cells[1]).html();
            var data = window.btoa(encodeURIComponent("rentOrderId=" + orderId));
            window.location.href = "rentOrderDetailManager.html?" + data;
            /*var url = "/orderInfo!showOrderDetail";
            var data = "rentOrderId="+orderId;
            sendRequest("post", url, data, showOrderDetailBack);*/
        };
    }

    changeAlign();

}


function showDetail(data,event) {

}
function showTable(){
    var data=null;
    var url="orderInfo!getAll";
    sendRequest("post", url, data, ShowAllBack);
}
function ShowAllBack(result){

    var json=JSON.parse(result);
    console.log(json);
    var obj = new Function("return" + json)();
    // if(jsonAll==null){
    // }
    jsonAll=obj;
    //alert(json.constructor);
    var totalNums = json.length;
    show(jsonAll);
    // showPageNum(jsonAll,totalNums);
}
// function showPageNum(json,totalNums){
//     var pageSet = pageset;
//     var totalPage = Math.ceil(totalNums/pageSet);//ceil 余数加1
//     var pagetotal = "共有" + "<strong>" + totalNums + " </strong> 条数据！";
//     var pageBar = "第" +page+ "页/共" + totalPage + "页" + "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     if(page<1){
//         pageBar += "<a href=\"javascript:pageSplite(" + 1 +"," + pageSet + ");\">首页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;"+
//             "<a href=\"javascript:pageSplite("+(page-1)+"," + pageSet+");\">上一页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     }else{
//         pageBar += "首页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
//     }
//     if(page<totalPage){
//         pageBar += "<a href=\"javascript:pageSplite(" + (page+1) + "," + pageSet + "); + \"> 下一页</a>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+"&nbsp;" +
//             "<a href=\"javascript:pageSplite("+totalPage+"," + pageSet+");\">尾页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
//     }else {
//         pageBar += "下一页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";;
//     }
//     document.getElementById("setf").innerHTML=pageBar;
// }
function pageSplite(page,pageSet,json) {
    var tb=document.getElementById("tb");
    var rowNum = tb.tBodies[0].rows.length;
    pageSet = pageset;
    if (pageSet==7)  pageSet=7;

    while(rowNum>1){
        table.deleteRow(1);
        rowNum--;
    }
    var num= table.rows.length;
    var totalNums = json.length;
    var totalPage = Math.ceil(totalNums/pageSet);//ceil 余数加1
    var begin = (page-1)*pageSet;
    var end = page*pageSet;
    end = end > totalNums ? totalNums : end;
    for(var i = begin; i < end; i ++){
        var row = tb.tBodies[0].insertRow();
        row.insertCell().innerHTML="<input type='checkbox'/>";
        row.insertCell().innerText=json[i].orderId;
        row.insertCell().innerText=json[i].custPhone;
        row.insertCell().innerText=json[i].accountNo;
        row.insertCell().innerText=json[i].productName;
        row.insertCell().innerText=json[i].orderTime;
        row.insertCell().innerText=json[i].consigeeAddr;
        row.insertCell().innerText=json[i].getOrConsigee;
        row.insertCell().innerText=json[i].buyPrice;
        row.insertCell().innerText=json[i].realPayPrice;
        if(json[i].orderState=="0"){
            row.insertCell().innerText = "未发货";
        }else if(json[i].orderState=="1"){
            row.insertCell().innerText = "已发货";
        }else if(json[i].orderState=="2") {
            row.insertCell().innerText = "已收货";
        }else if(json[i].orderState=="3") {
            row.insertCell().innerText = "未付款";
        }
        row.insertCell().innerHTML = "<a style='text-decoration:none' class='ml-5' onClick='purchase_edit()'>"+
            " <i class='Hui-iconfont'>&#xe6df;</i></a>&nbsp;&nbsp;<a style='text-decoration:none' class='ml-5' onClick='product_del(this,'10001')'" +
            " href='javascript:;' title='删除'>"+
            "<i class='Hui-iconfont'>&#xe6e2;</i></a>";
    }
    changeAlign();
    // tb.innerHTML = str;
    var pagetotal = "共有" + "<strong>" + totalNums + " </strong> 条数据！";
    var pageBar = "第" +page+ "页/共" + totalPage + "页" + "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    if(page<1){
        pageBar += "<a href=\"javascript:pageSplite(" + 1 +"," + pageSet + ");\">首页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;"+
            "<a href=\"javascript:pageSplite("+(page-1)+"," + pageSet+");\">上一页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    }else{
        pageBar += "首页"+ "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "上一页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
    }
    if(page<totalPage){
        pageBar += "<a href=\"javascript:pageSplite(" + (page+1) + "," + pageSet + "); + \"> 下一页</a>"+"&nbsp;"+"&nbsp;"+"&nbsp;"+"&nbsp;" +
            "<a href=\"javascript:pageSplite("+totalPage+"," + pageSet+");\">尾页</a>"+ "&nbsp;" + "&nbsp;"+ "&nbsp;"+ "&nbsp;";
    }else {
        pageBar += "下一页" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp"+ "尾页" + "&nbsp;" + "&nbsp;" + " &nbsp;" + "&nbsp;";
    }
    document.getElementById("set").innerHTML = pageBar;
    document.getElementById("total").innerHTML = pagetotal;
}
function SearchState(){
    var getstate = document.getElementById("state");
    var state = getstate.options[getstate.selectedIndex].value;
    var jsonState = [];
    if(state!=4){
        for(var i=0;i<jsonAll.length;i++){
            if(jsonAll[i].orderState==state){
                jsonState.push(jsonAll[i]);
            }
        }}else{
        jsonState=jsonAll;
    }
    show(jsonState);
}
function SearchOrder() {
    var name = document.getElementById("name").value;
    var tele = document.getElementById("tele").value;
    var timein = document.getElementById("timein").value;
    var timeout = document.getElementById("timeout").value;
    var dat = new Date();
    if(timeout!=""&&timein==""){
        alert("请输入查询日期开始时间");
        return;
    }
    if(timein!=""&&timeout==""){ timeout = dat.getTime(); }
    var date = "name="+ name + "&tele="+tele+"&timein=" + timein + "&timeout="+timeout;
    console.log(date);
    var url = "purchase!optionSelect";
    sendRequest("post", url, date, ShowAllBack);
}
function purchase_edit(orderId) {
    console.log(orderId);
    var jsona = [];
    for (var i=0;i<jsonAll.length;i++){
        if(jsonAll[i].orderId == orderId){
            jsona.push(jsonAll[i]);
        }
    }
    var jsonb=JSON.stringify(jsona);
    console.log(jsonb);
    sessionStorage.setItem('jsona', jsonb);
    sessionStorage.setItem('jsonAll', jsonAll);
    window.location.href = "purchase_edit.html";
}



